package com.zd.mole.site.mohurd.process;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zd.mole.process.ProcessHandler;
import com.zd.mole.site.mohurd.entity.Ot_procomplete_info;
import com.zd.mole.site.mohurd.repository.OtCompanyInfoRepository;
import com.zd.mole.site.mohurd.repository.OtPersonInfoRepository;
import com.zd.mole.task.TaskStatus;
import com.zd.mole.task.entity.Task;
import com.zd.mole.task.service.TaskService;
import com.zd.mole.utils.RegexUtils;

/**
 * 项目信息/验收竣工备案
 * @author ZhangDi
 * 
 */
@Component
@Transactional
public class MohurdDataserviceQueryProjectBafinishInfoHandler implements ProcessHandler {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@PersistenceContext 
	private EntityManager em;
	@Resource
	private OtCompanyInfoRepository otCompanyInfoRepository;
	@Resource
	private OtPersonInfoRepository otPersonInfoRepository;
	@Resource
	private TaskService taskService;
	
	@Override
	public List<Task> handler(Task task, String text) {

		Ot_procomplete_info p = new Ot_procomplete_info();
		String id = task.getRequestUrl().replaceFirst("/dataservice/query/project/bafinishInfo/", "");
		p.setId(id);
		p.setProjectNo(task.getCode());
		p.setCompleteBakCode(RegexUtils.find("<td data-header=\"竣工备案编号\">" + INNER_HTML + "</td>", text));
		p.setProviBakCode(RegexUtils.find("<td data-header=\"省级竣工备案编号\">" + INNER_HTML + "</td>", text));
		p.setFactSum(RegexUtils.find("<td data-header=\"实际造价（万元）\">" + INNER_HTML + "</td>", text));
		p.setFactArea(RegexUtils.find("<td data-header=\"实际面积（平方米）\">" + INNER_HTML + "</td>", text));
		p.setRealBuiltModel(RegexUtils.find("<td  colspan=\"3\" data-header=\"实际建设规模\">" + INNER_HTML + "</td>", text));
		p.setStrutInfo(RegexUtils.find("<td  colspan=\"3\" data-header=\"结构体系\">-</td>" + INNER_HTML + "</td>", text));
		p.setFactOpenDate(RegexUtils.find("<td data-header=\"实际开工日期\">" + INNER_HTML + "</td>", text));
		p.setFactCompleteDate(RegexUtils.find("<td data-header=\"实际竣工验收日期\">" + INNER_HTML + "</td>", text));
		String recordDate = RegexUtils.find("<td data-header=\"记录登记时间\">" + INNER_HTML + "</td>", text);
		p.setCreate_date(new Date());
		try {
			p.setRecordDate(new SimpleDateFormat("yyyy-MM-dd").parse(recordDate));
			p.setCreate_date(p.getRecordDate());
		} catch (ParseException e) {
			log.error("决定日期字段，日期转换错误原始字段为[{}]", e.getMessage());
//			e.printStackTrace();
		}
		p.setRemarks(RegexUtils.find("<td data-header=\"备注\">" + INNER_HTML + "</td>", text));
		p.setCreate_by("1");
		p.setUpdate_by("1");
		p.setUpdate_date(new Date());
		p.setDel_flag("0");
		
		String designUnitId = RegexUtils.find("<td data-header=\"涉及单位\">设计单位</td>"
				+ "\\s*<td data-header=\"企业名称\">"
				+ "\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>" 
				+ "\\s*</td>", text);
		p.setDesignUnit(designUnitId);
		if(!"".equals(designUnitId)) {
			p.setDesignUnitName(RegexUtils.find("<td data-header=\"涉及单位\">设计单位</td>"
					+ "\\s*<td data-header=\"企业名称\">"
					+ "\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>" 
					+ "\\s*</td>", text));
			addCompanyTask(task, designUnitId);
		} else {
			p.setManagerUnitName(RegexUtils.find("<td data-header=\"涉及单位\">设计单位</td>"
					+ "\\s*<td data-header=\"企业名称\">"
					+ "\\s*" + INNER_HTML 
					+ "\\s*</td>", text));
		}
		
		String managerUnitId = RegexUtils.find("<td data-header=\"涉及单位\">监理单位</td>"
				+ "\\s*<td data-header=\"企业名称\">"
				+ "\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>" 
				+ "\\s*</td>", text);
		p.setManagerUnit(managerUnitId);
		if(!"".equals(managerUnitId)) {
			p.setManagerUnitName(RegexUtils.find("<td data-header=\"涉及单位\">监理单位</td>"
					+ "\\s*<td data-header=\"企业名称\">"
					+ "\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>" 
					+ "\\s*</td>", text));
			addCompanyTask(task, managerUnitId);
		} else {
			p.setManagerUnitName(RegexUtils.find("<td data-header=\"涉及单位\">监理单位</td>"
					+ "\\s*<td data-header=\"企业名称\">"
					+ "\\s*" + INNER_HTML 
					+ "\\s*</td>", text));
		}
		
		String buildUnitId = RegexUtils.find("<td data-header=\"涉及单位\">施工单位</td>"
				+ "\\s*<td data-header=\"企业名称\">"
				+ "\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>" 
				+ "\\s*</td>", text);
		p.setBuildUnit(buildUnitId);
		if(!"".equals(buildUnitId)) {
			p.setBuildUnitName(RegexUtils.find("<td data-header=\"涉及单位\">施工单位</td>"
					+ "\\s*<td data-header=\"企业名称\">"
					+ "\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>" 
					+ "\\s*</td>", text));
		} else {
			p.setBuildUnitName(RegexUtils.find("<td data-header=\"涉及单位\">施工单位</td>"
					+ "\\s*<td data-header=\"企业名称\">"
					+ "\\s*" + INNER_HTML 
					+ "\\s*</td>", text));
		}
		String projectManagerId = RegexUtils.find("<td data-header=\"担任角色\">项目经理</td>" 
				+ "\\s*<td data-header=\"姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML
				+ "</a>\\s*</td>", text);
		p.setProjectManagerId(projectManagerId);
		if(!"".equals(projectManagerId)) {
			p.setProjectManagerName(RegexUtils.find("<td data-header=\"担任角色\">项目经理</td>" 
				+ "\\s*<td data-header=\"姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/\\d+\" target=\"_blank\">" + INNER_HTML
				+ "</a>\\s*</td>", text));
			addPersonTask(task, projectManagerId);
		} else {
			p.setProjectManagerName(RegexUtils.find("<td data-header=\"担任角色\">项目经理</td>" 
					+ "\\s*<td data-header=\"姓名\">\\s*" + INNER_HTML
					+ "\\s*</td>", text));
		}
		p.setProjectManagerCardNo(RegexUtils.find("<td data-header=\"担任角色\">项目经理</td>" 
				+ "\\s*<td data-header=\"姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/(\\d+)\" target=\"_blank\">[\\s\\S]*?"
				+ "</a>\\s*</td><td data-header=\"证件号码\">" + INNER_HTML + "</td>", text));
			
		String projectInspectorId = RegexUtils.find("<td data-header=\"担任角色\">总监理工程师</td>" 
				+ "\\s*<td data-header=\"姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML
				+ "</a>\\s*</td>", text);
		p.setProjectInspectorId(projectInspectorId);
		if(!"".equals(projectInspectorId)) {
			p.setProjectInspectorName(RegexUtils.find("<td data-header=\"担任角色\">总监理工程师</td>" 
				+ "\\s*<td data-header=\"姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/\\d+\" target=\"_blank\">" + INNER_HTML
				+ "</a>\\s*</td>", text));
			addPersonTask(task, projectInspectorId);
		} else {
			p.setProjectInspectorName(RegexUtils.find("<td data-header=\"担任角色\">总监理工程师</td>" 
					+ "\\s*<td data-header=\"姓名\">\\s*" + INNER_HTML
					+ "\\s*</td>", text));
		}
		p.setProjectInspectorCardNo(RegexUtils.find("<td data-header=\"担任角色\">总监理工程师</td>" 
				+ "\\s*<td data-header=\"姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/(\\d+)\" target=\"_blank\">[\\s\\S]*?"
				+ "</a>\\s*</td><td data-header=\"证件号码\">" + INNER_HTML + "</td>", text));
		em.merge(p);
		
		return null;
	}
	
	private void addCompanyTask(Task task, String companyId) {
		if(!"".equals(companyId)) {
			if(!otCompanyInfoRepository.existsById(companyId)) {
				Task nt = new Task();
				nt.setCode(companyId);
				nt.setHostUrl(task.getHostUrl());
				nt.setRequestUrl("/dataservice/query/comp/compDetail/" + companyId);
				nt.setProcessHandlerClassName("MohurdDataserviceQueryCompCompDetailHandler");
				nt.setStatus(TaskStatus.Ready);
				taskService.save(nt);
			}
		}
	}
	
	private void addPersonTask(Task task, String personId) {
		if(!"".equals(personId)) {
			if(!otPersonInfoRepository.existsById(personId)) {
				Task nt = new Task();
				nt.setCode(personId);
				nt.setHostUrl(task.getHostUrl());
				nt.setRequestUrl("/dataservice/query/staff/staffDetail/" + personId);
				nt.setProcessHandlerClassName("MohurdDataserviceQueryStaffstaffDetailHandler");
				nt.setStatus(TaskStatus.Ready);
				taskService.save(nt);
			}
		}
	}

}
